package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Transactional
@Service
public class DishServiceimpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;



    @Override
    public void add(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.InsertDish(dish);
        //将插入菜品的id获取出来
        Long id = dish.getId();
        if(flavors!=null&& flavors.size()!=0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            dishFlavorMapper.InsertDishFlavor(flavors);
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult= new PageResult();
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        pageResult.setTotal(page.getTotal());
        List<DishVO> result = page.getResult();
        for (DishVO dishVO : result) {
            dishVO.setFlavors(dishFlavorMapper.getbydisid(dishVO.getId()));
        }
        pageResult.setRecords(result);
        return pageResult;
    }


    @Override
    public void delete(List<Long> ids) {

        //起售的不能删除
        for (Long id : ids) {
            DishVO dish = dishMapper.getbyid(id);
            if(Objects.equals(dish.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(dish.getName()+"起售的菜品不允许删除");
            }
        }
        //有套餐的不能删除
        for (Long id : ids) {
            long num = setmealDishMapper.countbydishid(id);
            if(num!=0){
                throw new DeletionNotAllowedException(dishMapper.getbyid(id).getName()+ "有套餐的菜品不允许删除");
            }
        }
            //删除菜品
        dishMapper.deletebyids(ids);
            //删除菜品的口味
        for (Long id : ids)
            dishFlavorMapper.deletebydishid(id);
    }

    @Override
    public DishVO getbyid(Long id) {
        DishVO dishVO = dishMapper.getbyid(id);
        dishVO.setFlavors(dishFlavorMapper.getbydisid(id));
        return dishVO;
    }

    @Override
    public List <DishVO> getbycategoryId(Long categoryId) {
        List <DishVO> dishVOs = dishMapper.getbycategoryId(categoryId);
        for (DishVO dishVO : dishVOs)
            dishVO.setFlavors(dishFlavorMapper.getbydisid(dishVO.getId()));
        return dishVOs;
    }

    @Override
    public void startend(Integer status, Long id) {
        Dish dish=Dish.builder().status(status).id(id).build();
        dishMapper.updatestatus(dish);
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFlavorMapper.deletebydishid(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()!=0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.InsertDishFlavor(flavors);
        }
    }
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getbydisid(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
